/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modules.legislation.law;

import datatypes.Antenna;
import modules.legislation.PathlossInterface;
import modules.legislation.datatypes.AntennaAndDistance;
import tools.math.MathTools;
import tools.settings.SettingsMissingException;

/**
 * BELANGRIJK : de REGELGEVING voor belgie is gebaseerd op E = de elektro(magnetische) veldsterkte.
 * E = [Volt / meter ]
 *
 *
 * BELGIUM  WORKS WITH E
 *
 *
 * @author Pieter Backaert
 */
public class Belgium implements LawInterface {
   private double[] limitsForEachAntenna;
   private static PathlossInterface instanceOfPathlossInterface;

    private double SAR_value;


    public Belgium(PathlossInterface instanceOfPathlossInterface){
        this.instanceOfPathlossInterface = instanceOfPathlossInterface;
    }


    public double getSAR_value() {
        return SAR_value;
    }

    public void setSAR_value(double SAR_value) {
        this.SAR_value = SAR_value;
    }

    public void initLimitsForEachAntenna(AntennaAndDistance[] ant){
        limitsForEachAntenna = new double[ant.length];

        for(int i=0 ; i<ant.length ; i++){
            AntennaAndDistance aad = ant[i];
            Antenna a = aad.getAntenna();
            limitsForEachAntenna[i] = giveELimit(a);
        }
    }

    public void setLimitForAntenna(int index, double x){
        limitsForEachAntenna[index] = x;
    }

    public double getLimitForAntenna(int index){
        return limitsForEachAntenna[index];
    }







    /**
     * Checks for an array of Antennas whether the legislation is ok.
     *
     * @param ant
     * @return
     */

    // frequentie is uitgedruk in Mhz
    public boolean checkLegislation(AntennaAndDistance[] ant) throws SettingsMissingException {
        //bereken max. waarde voor iedere antenna
        initLimitsForEachAntenna(ant);

        double totalPercentage=0;

        for(int x=0; x<ant.length ; x++){

            AntennaAndDistance aad = ant[x];

            Antenna a = aad.getAntenna();
            double distance = aad.getDistance();

            double result = calculate_E_field_strength(a , distance);

            if(result==0 || limitsForEachAntenna[x]==0)
                totalPercentage += 0;
            else
                totalPercentage += Math.pow((result/limitsForEachAntenna[x]),2);
        }


        setSAR_value(totalPercentage);

        //check
        if( totalPercentage <= 1 )
            return true;
        else
            return false;
    }

    public double giveELimit(Antenna a){
        double freq = a.getFrequency();
        if(freq <10)
            return 0;
        else if(freq>=10 && freq<400)
            return 13.7;
        else if(freq>=400 && freq<2000)
            return (0.686 * Math.sqrt(freq));
        else //if(freq>= 2000)
            return 30.7;
    }

    public double calculate_E_field_strength(Antenna a , double i) {
        double E = 0;
        try {
            double pl = instanceOfPathlossInterface.getDbPathLoss(a, i);
            double EIRP = a.getPower() + a.getGain();

            E = MathTools.calculate_simple_E_from_PathLoss(EIRP, pl, a.getFrequency() );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return E;
    }
}
